package br.com.sca.interfaces;

import br.com.sca.SistemaCadastroAluno;
import br.com.sca.dao.EmpresaDAO;
import br.com.sca.entidades.Aluno;
import br.com.sca.entidades.Empresa;
import br.com.sca.util.Constantes;
import java.text.ParseException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;

public class EmpresaFrame extends javax.swing.JInternalFrame {

    private DefaultTableModel tableModel;
    private ListSelectionModel listModel;
    private List<Empresa> empresas;
    private int modo = 2;
    private ConvenioFrame convenioFrame;

    /**
     * Creates new form AlunoFrame
     */
    public EmpresaFrame() {
        initComponents();
        defineModelo();
        desabilitaCampos();
        desabilitaBotoes();
        atualizaTabela();

        btSelecionar.setVisible(false);
    }

    public EmpresaFrame(ConvenioFrame convenioFrame) {
        initComponents();
        defineModelo();
        desabilitaCampos();
        desabilitaBotoes();
        atualizaTabela();

        btSelecionar.setVisible(true);

        this.convenioFrame = convenioFrame;
    }

    private void defineModelo() {
        /**
         * Define os dados incluso na tabela
         */
        tableModel = (DefaultTableModel) tbEmpresa.getModel();

        /**
         * Escuta a tabela
         */
        listModel = tbEmpresa.getSelectionModel();
        listModel.addListSelectionListener(new ListSelectionListener() {

            /**
             * Executa quando um linha da tabela e executado
             *
             * @param e
             */
            @Override
            public void valueChanged(ListSelectionEvent e) {
                mostraDetalheEmpresa();
            }
        });

        try {
            /*Cria a mascara para CEP*/
            MaskFormatter cepMask = new MaskFormatter("##.###-###");
            cepMask.setValueContainsLiteralCharacters(false);
            DefaultFormatterFactory formatterCEP = new DefaultFormatterFactory(cepMask);
            ftCep.setFormatterFactory(formatterCEP);

            /*Cria a mascara para o CNPJ*/
            MaskFormatter cnpjMafk = new MaskFormatter("##.###.###/####-##");
            cnpjMafk.setValueContainsLiteralCharacters(false);
            DefaultFormatterFactory formatterCNPJ = new DefaultFormatterFactory(cnpjMafk);
            ftCNPJ.setFormatterFactory(formatterCNPJ);

            /*Cria mascara para data*/
            /*MaskFormatter dataMask = new MaskFormatter("##/##/####");
             dataMask.setValueContainsLiteralCharacters(false);
             DefaultFormatterFactory formatterData = new DefaultFormatterFactory(dataMask);
             ftNascimento.setFormatterFactory(formatterData);*/
            /*Cria mascara para telefone*/
            MaskFormatter telMask = new MaskFormatter("(##)####-####");
            telMask.setValueContainsLiteralCharacters(false);
            DefaultFormatterFactory formateTel = new DefaultFormatterFactory(telMask);
            ftTelefone.setFormatterFactory(formateTel);
            ftCelular.setFormatterFactory(formateTel);
        } catch (ParseException ex) {
            Logger.getLogger(EmpresaFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Mosta o detalhe do aluno da linha selecionada
     */
    private void mostraDetalheEmpresa() {
        if (tbEmpresa.getSelectedRow() != -1) {
            int linha = tbEmpresa.getSelectedRow();
            txRazaoSocial.setText(empresas.get(linha).getRazaoSicial());
            ftInscEstadual.setValue(empresas.get(linha).getInscEstadual());
            txNomeFantasia.setText(empresas.get(linha).getNomeFantasia());
            ftCNPJ.setValue(empresas.get(linha).getCnpj());
            txEndereco.setText(empresas.get(linha).getEndereco());
            txNumero.setText(empresas.get(linha).getNumero());
            txBairro.setText(empresas.get(linha).getBairro());
            txCidade.setText(empresas.get(linha).getCidade());
            ftCep.setValue(empresas.get(linha).getCep());
            cbUf.setSelectedItem(empresas.get(linha).getUf());
            ftTelefone.setValue(empresas.get(linha).getTelefone());
            ftCelular.setValue(empresas.get(linha).getCelular());
            txContato.setText(empresas.get(linha).getNomeContato());
            txEmail.setText(empresas.get(linha).getEmail());
        } else {
            limpaTela();
        }
    }

    /**
     * Atualiza a tabela com as inforamações da lista de Alunos
     */
    private void atualizaTabela() {
        EmpresaDAO ad = new EmpresaDAO();
        try {
            empresas = ad.listaEmpresas();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Erro ao carregar a lista de alunos", "Erro", JOptionPane.ERROR_MESSAGE);
        }

        int numLinhas = tableModel.getRowCount();
        for (int i = 0; i < numLinhas; i++) {
            tableModel.removeRow(0);
        }

        for (int i = 0; i < empresas.size(); i++) {
            tableModel.insertRow(i, new Object[]{empresas.get(i).getId(), empresas.get(i).getNomeFantasia(), empresas.get(i).getCnpj(), empresas.get(i).getNomeContato(), empresas.get(i).getTelefone(), empresas.get(i).getEmail()});
        }
    }

    private void desabilitaBotoes() {

        btSalvar.setEnabled(false);
        btCancelar.setEnabled(false);

        btNovo.setEnabled(true);
        btAlterar.setEnabled(true);
        btExcluir.setEnabled(true);
        btPesquisar.setEnabled(true);
    }

    private void habilitaBotoes() {

        btSalvar.setEnabled(true);
        btCancelar.setEnabled(true);

        btNovo.setEnabled(false);
        btAlterar.setEnabled(false);
        btExcluir.setEnabled(false);
        btPesquisar.setEnabled(false);
    }

    /**
     * Habilita os campos para digitação
     */
    private void habilitaCampos() {

        txRazaoSocial.setEditable(true);
        txNomeFantasia.setEditable(true);
        ftCNPJ.setEditable(true);
        ftInscEstadual.setEditable(true);
        txEndereco.setEditable(true);
        txNumero.setEditable(true);
        txBairro.setEditable(true);
        txCidade.setEditable(true);
        ftCep.setEditable(true);
        cbUf.setEnabled(true);
        ftTelefone.setEditable(true);
        ftCelular.setEditable(true);
        txContato.setEditable(true);
        txEmail.setEditable(true);
    }

    /**
     * Desabilita os campos pra digitação
     */
    private void desabilitaCampos() {

        txRazaoSocial.setEditable(false);
        txNomeFantasia.setEditable(false);
        ftCNPJ.setEditable(false);
        ftInscEstadual.setEditable(false);
        txEndereco.setEditable(false);
        txNumero.setEditable(false);
        txBairro.setEditable(false);
        txCidade.setEditable(false);
        ftCep.setEditable(false);
        cbUf.setEnabled(false);
        ftTelefone.setEditable(false);
        ftCelular.setEditable(false);
        txContato.setEditable(false);
        txEmail.setEditable(false);
    }

    /**
     * Limpa os campos
     */
    private void limpaTela() {

        txRazaoSocial.setText("");
        txNomeFantasia.setText("");
        ftCNPJ.setValue(null);
        ftInscEstadual.setValue(null);
        txEndereco.setText("");
        txNumero.setText("");
        txBairro.setText("");
        txCidade.setText("");
        ftCep.setValue(null);
        cbUf.setSelectedIndex(0);
        ftTelefone.setValue(null);
        ftCelular.setValue(null);
        txContato.setText("");
        txEmail.setText("");

    }

    private void adicionarEmpresa() {
        if (txRazaoSocial.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Campo nome é obrigatorio", "Aviso", JOptionPane.WARNING_MESSAGE);
            txRazaoSocial.requestFocus();
        } else {
            EmpresaDAO ad = new EmpresaDAO();
            Empresa empresa = new Empresa();

            empresa.setRazaoSicial(txRazaoSocial.getText().trim());
            empresa.setNomeFantasia(txNomeFantasia.getText().trim());
            empresa.setCnpj((String) ftCNPJ.getValue());
            empresa.setInscEstadual((String) ftInscEstadual.getValue());
            empresa.setEndereco(txEndereco.getText().trim());
            empresa.setNumero(txNumero.getText().trim());
            empresa.setBairro(txBairro.getText().trim());
            empresa.setCidade(txCidade.getText().trim());
            empresa.setCep((String) ftCep.getValue());
            empresa.setUf((String) cbUf.getSelectedItem());
            empresa.setTelefone((String) ftTelefone.getValue());
            empresa.setCelular((String) ftCelular.getValue());
            empresa.setNomeContato(txContato.getText().trim());
            empresa.setEmail(txEmail.getText().trim());
            empresa.setAtivo(true);

            try {
                ad.salvar(empresa);
                atualizaTabela();
                desabilitaBotoes();
                desabilitaCampos();
                limpaTela();
                JOptionPane.showMessageDialog(this, "Empresa registrado com sucesso!", "Informação", JOptionPane.INFORMATION_MESSAGE);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Erro ao tentar gravar o registro", "Erro", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void alterarEmpresa() {
        if (txRazaoSocial.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Campo nome é obrigatorio", "Aviso", JOptionPane.WARNING_MESSAGE);
            txRazaoSocial.requestFocus();
        } else {
            EmpresaDAO ad = new EmpresaDAO();
            Empresa empresa = new Empresa();
            empresa.setId(empresas.get(tbEmpresa.getSelectedRow()).getId());
            empresa.setRazaoSicial(txRazaoSocial.getText().trim());
            empresa.setNomeFantasia(txNomeFantasia.getText().trim());
            empresa.setCnpj((String) ftCNPJ.getValue());
            empresa.setInscEstadual((String) ftInscEstadual.getValue());
            empresa.setEndereco(txEndereco.getText().trim());
            empresa.setNumero(txNumero.getText().trim());
            empresa.setBairro(txBairro.getText().trim());
            empresa.setCidade(txCidade.getText().trim());
            empresa.setCep((String) ftCep.getValue());
            empresa.setUf((String) cbUf.getSelectedItem());
            empresa.setTelefone((String) ftTelefone.getValue());
            empresa.setCelular((String) ftCelular.getValue());
            empresa.setNomeContato(txContato.getText().trim());
            empresa.setEmail(txEmail.getText().trim());
            empresa.setAtivo(true);

            try {
                ad.alterar(empresa);
                atualizaTabela();
                desabilitaBotoes();
                desabilitaCampos();
                limpaTela();
                JOptionPane.showMessageDialog(this, "Empresa alterado com sucesso!", "Informação", JOptionPane.INFORMATION_MESSAGE);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Erro ao tentar alterar o registro", "Erro", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void excluirEmpresa() {
        if (tbEmpresa.getSelectedRow() != -1) {
            EmpresaDAO ad = new EmpresaDAO();
            Empresa empresa = new Empresa();
            empresa = empresas.get(tbEmpresa.getSelectedRow());
            empresa.setAtivo(false);
            ad.alterar(empresa);
            JOptionPane.showMessageDialog(this, "Empresa excluido com sucesso!", "Informação", JOptionPane.INFORMATION_MESSAGE);
            limpaTela();
            desabilitaBotoes();
            desabilitaCampos();
            atualizaTabela();
        } else {
            JOptionPane.showMessageDialog(this, "Selecione um empresa para exclusão!", "Erro", JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        pnCabecalho = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        pnDados = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txRazaoSocial = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txNomeFantasia = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txEndereco = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txNumero = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txBairro = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txCidade = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        txContato = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        ftCep = new javax.swing.JFormattedTextField();
        jLabel18 = new javax.swing.JLabel();
        txEmail = new javax.swing.JTextField();
        ftInscEstadual = new javax.swing.JFormattedTextField();
        cbUf = new javax.swing.JComboBox();
        ftCNPJ = new javax.swing.JFormattedTextField();
        ftTelefone = new javax.swing.JFormattedTextField();
        ftCelular = new javax.swing.JFormattedTextField();
        pnGrid = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbEmpresa = new javax.swing.JTable();
        jLabel19 = new javax.swing.JLabel();
        txPesquisa = new javax.swing.JTextField();
        btPesquisar = new javax.swing.JButton();
        pnButton = new javax.swing.JPanel();
        btNovo = new javax.swing.JButton();
        btAlterar = new javax.swing.JButton();
        btExcluir = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        btSelecionar = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("SCA - Cadastro de Empresas");
        setName(""); // NOI18N
        setPreferredSize(new java.awt.Dimension(860, 600));
        getContentPane().setLayout(new java.awt.GridBagLayout());

        pnCabecalho.setBackground(new java.awt.Color(0, 51, 255));

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Cadastro de Empresas");
        pnCabecalho.add(jLabel1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(pnCabecalho, gridBagConstraints);

        pnDados.setBackground(javax.swing.UIManager.getDefaults().getColor("CheckBox.light"));

        jLabel2.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel2.setText("Razão Social.:");

        txRazaoSocial.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txRazaoSocial.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                txRazaoSocialPropertyChange(evt);
            }
        });
        txRazaoSocial.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txRazaoSocialKeyReleased(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel3.setText("Nome Fantasia.:");

        jLabel4.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel4.setText("Insc. Estadual.:");

        txNomeFantasia.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txNomeFantasia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txNomeFantasiaKeyReleased(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel6.setText("CNPJ.:");

        jLabel8.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel8.setText("Endereço.:");

        txEndereco.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txEndereco.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txEnderecoKeyReleased(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel9.setText("Numero.:");

        txNumero.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        jLabel10.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel10.setText("Bairro.:");

        txBairro.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txBairro.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txBairroKeyReleased(evt);
            }
        });

        jLabel11.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel11.setText("Cidade.:");

        txCidade.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txCidade.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txCidadeKeyReleased(evt);
            }
        });

        jLabel12.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel12.setText("UF.:");

        jLabel13.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel13.setText("Telefone.:");

        jLabel14.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel14.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel14.setText("Celular.:");

        jLabel15.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel15.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel15.setText("Contato.:");

        txContato.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txContato.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txContatoKeyReleased(evt);
            }
        });

        jLabel17.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel17.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel17.setText("CEP.:");

        ftCep.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        jLabel18.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel18.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel18.setText("Email.:");

        txEmail.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txEmail.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txEmailKeyReleased(evt);
            }
        });

        ftInscEstadual.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));
        ftInscEstadual.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        cbUf.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        cbUf.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-", "AC", "AL", "AP", "AM", "BA", "CE", "DF", "ES", "GO", "MA", "MT", "MS", "MG", "PA", "PB", "PR", "PE", "PI", "RJ", "RN", "RS", "RO", "RR", "SC", "SE", "SP", "TO" }));

        ftCNPJ.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        ftTelefone.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        ftCelular.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        javax.swing.GroupLayout pnDadosLayout = new javax.swing.GroupLayout(pnDados);
        pnDados.setLayout(pnDadosLayout);
        pnDadosLayout.setHorizontalGroup(
            pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnDadosLayout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(pnDadosLayout.createSequentialGroup()
                        .addComponent(jLabel18)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 398, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel15)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txContato))
                    .addGroup(pnDadosLayout.createSequentialGroup()
                        .addComponent(jLabel17)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ftCep, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbUf, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel13)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ftTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel14)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ftCelular, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnDadosLayout.createSequentialGroup()
                        .addGroup(pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnDadosLayout.createSequentialGroup()
                                .addGap(54, 54, 54)
                                .addComponent(txNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txBairro, javax.swing.GroupLayout.PREFERRED_SIZE, 322, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel11))
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txCidade))
                    .addGroup(pnDadosLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txRazaoSocial, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txNomeFantasia, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnDadosLayout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(3, 3, 3)
                        .addComponent(ftCNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ftInscEstadual, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txEndereco)))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        pnDadosLayout.setVerticalGroup(
            pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnDadosLayout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txRazaoSocial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txNomeFantasia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ftInscEstadual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ftCNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel17, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ftCep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbUf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ftTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ftCelular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel18, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txContato, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23))
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(pnDados, gridBagConstraints);

        pnGrid.setBackground(javax.swing.UIManager.getDefaults().getColor("CheckBox.light"));
        pnGrid.setLayout(new java.awt.GridBagLayout());

        tbEmpresa.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Nome Fantasia", "CNPJ", "Contato", "Telefone", "E-Mail"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbEmpresa.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tbEmpresa);
        if (tbEmpresa.getColumnModel().getColumnCount() > 0) {
            tbEmpresa.getColumnModel().getColumn(0).setMaxWidth(50);
            tbEmpresa.getColumnModel().getColumn(1).setMaxWidth(240);
            tbEmpresa.getColumnModel().getColumn(2).setMaxWidth(140);
            tbEmpresa.getColumnModel().getColumn(3).setMaxWidth(160);
            tbEmpresa.getColumnModel().getColumn(4).setMaxWidth(100);
            tbEmpresa.getColumnModel().getColumn(5).setMaxWidth(220);
        }

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnGrid.add(jScrollPane1, gridBagConstraints);

        jLabel19.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel19.setForeground(new java.awt.Color(204, 0, 0));
        jLabel19.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel19.setText("Pequisar por nome fantasia.:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnGrid.add(jLabel19, gridBagConstraints);

        txPesquisa.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txPesquisa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txPesquisaKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnGrid.add(txPesquisa, gridBagConstraints);

        btPesquisar.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        btPesquisar.setText("Pesquisar");
        btPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnGrid.add(btPesquisar, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(pnGrid, gridBagConstraints);

        pnButton.setLayout(new java.awt.GridBagLayout());

        btNovo.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        btNovo.setText("Novo");
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnButton.add(btNovo, gridBagConstraints);

        btAlterar.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        btAlterar.setText("Alterar");
        btAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAlterarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnButton.add(btAlterar, gridBagConstraints);

        btExcluir.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        btExcluir.setText("Excluir");
        btExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExcluirActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnButton.add(btExcluir, gridBagConstraints);

        btCancelar.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        btCancelar.setText("Cancelar");
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnButton.add(btCancelar, gridBagConstraints);

        btSalvar.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        btSalvar.setText("Salvar");
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnButton.add(btSalvar, gridBagConstraints);

        btSelecionar.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        btSelecionar.setText("Selecionar");
        btSelecionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSelecionarActionPerformed(evt);
            }
        });
        pnButton.add(btSelecionar, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(pnButton, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        if (modo == Constantes.INSERT_MODEL) {
            adicionarEmpresa();
        } else if (modo == Constantes.EDIT_MODEL) {
            alterarEmpresa();
        }
    }//GEN-LAST:event_btSalvarActionPerformed

    private void pesquisarAlunoPorNome() {
        if (txPesquisa.getText().length() > 2) {
            EmpresaDAO ad = new EmpresaDAO();
            try {
                empresas = ad.pesquisarEmpresasPorNome(txPesquisa.getText());
                txPesquisa.setText("");
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Erro ao carregar a lista de emprea", "Erro", JOptionPane.ERROR_MESSAGE);
            }

            int numLinhas = tableModel.getRowCount();
            for (int i = 0; i < numLinhas; i++) {
                tableModel.removeRow(0);
            }

            for (int i = 0; i < empresas.size(); i++) {
                tableModel.insertRow(i, new Object[]{empresas.get(i).getId(), empresas.get(i).getNomeFantasia(), empresas.get(i).getCnpj(), empresas.get(i).getNomeContato(), empresas.get(i).getTelefone(), empresas.get(i).getEmail()});
            }
        } else {
            JOptionPane.showMessageDialog(this, "Digite no mínimo 3 caracteres para realizar a pesquisa", "Informação", JOptionPane.INFORMATION_MESSAGE);
        }

    }

    private void btPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisarActionPerformed
        pesquisarAlunoPorNome();
    }//GEN-LAST:event_btPesquisarActionPerformed

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        modo = Constantes.INSERT_MODEL;
        habilitaBotoes();
        habilitaCampos();
        txRazaoSocial.requestFocus();
    }//GEN-LAST:event_btNovoActionPerformed

    private void btAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAlterarActionPerformed
        if (tbEmpresa.getSelectedRow() != -1) {
            modo = Constantes.EDIT_MODEL;
            habilitaBotoes();
            habilitaCampos();
        } else {
            JOptionPane.showMessageDialog(null, "Selecione um aluno para alteração", "Atenção", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btAlterarActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        desabilitaBotoes();
        desabilitaCampos();
        limpaTela();
        atualizaTabela();
        modo = 2;
    }//GEN-LAST:event_btCancelarActionPerformed

    private void btExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExcluirActionPerformed
        excluirEmpresa();
    }//GEN-LAST:event_btExcluirActionPerformed

    private void txRazaoSocialPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_txRazaoSocialPropertyChange

    }//GEN-LAST:event_txRazaoSocialPropertyChange

    private void txRazaoSocialKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txRazaoSocialKeyReleased
        txRazaoSocial.setText(txRazaoSocial.getText().toUpperCase());
    }//GEN-LAST:event_txRazaoSocialKeyReleased

    private void txNomeFantasiaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txNomeFantasiaKeyReleased
        txNomeFantasia.setText(txNomeFantasia.getText().toUpperCase());
    }//GEN-LAST:event_txNomeFantasiaKeyReleased

    private void txEnderecoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txEnderecoKeyReleased
        txEndereco.setText(txEndereco.getText().toUpperCase());
    }//GEN-LAST:event_txEnderecoKeyReleased

    private void txBairroKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txBairroKeyReleased
        txBairro.setText(txBairro.getText().toUpperCase());
    }//GEN-LAST:event_txBairroKeyReleased

    private void txCidadeKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txCidadeKeyReleased
        txCidade.setText(txCidade.getText().toUpperCase());
    }//GEN-LAST:event_txCidadeKeyReleased

    private void txContatoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txContatoKeyReleased
        txContato.setText(txContato.getText().toUpperCase());
    }//GEN-LAST:event_txContatoKeyReleased

    private void txEmailKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txEmailKeyReleased
        txEmail.setText(txEmail.getText().toLowerCase());
    }//GEN-LAST:event_txEmailKeyReleased

    private void txPesquisaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txPesquisaKeyReleased
        txPesquisa.setText(txPesquisa.getText().toUpperCase());
    }//GEN-LAST:event_txPesquisaKeyReleased

    private void btSelecionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSelecionarActionPerformed
        if (tbEmpresa.getSelectedRow() != -1) {
            convenioFrame.setEmpresa(empresas.get(tbEmpresa.getSelectedRow()));
            this.dispose();
            convenioFrame.toFront();
        } else {
            JOptionPane.showMessageDialog(this, "Empresa não foi selecionada", "Aviso", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btSelecionarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAlterar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btExcluir;
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btPesquisar;
    private javax.swing.JButton btSalvar;
    private javax.swing.JButton btSelecionar;
    private javax.swing.JComboBox cbUf;
    private javax.swing.JFormattedTextField ftCNPJ;
    private javax.swing.JFormattedTextField ftCelular;
    private javax.swing.JFormattedTextField ftCep;
    private javax.swing.JFormattedTextField ftInscEstadual;
    private javax.swing.JFormattedTextField ftTelefone;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnButton;
    private javax.swing.JPanel pnCabecalho;
    private javax.swing.JPanel pnDados;
    private javax.swing.JPanel pnGrid;
    private javax.swing.JTable tbEmpresa;
    private javax.swing.JTextField txBairro;
    private javax.swing.JTextField txCidade;
    private javax.swing.JTextField txContato;
    private javax.swing.JTextField txEmail;
    private javax.swing.JTextField txEndereco;
    private javax.swing.JTextField txNomeFantasia;
    private javax.swing.JTextField txNumero;
    private javax.swing.JTextField txPesquisa;
    private javax.swing.JTextField txRazaoSocial;
    // End of variables declaration//GEN-END:variables

}
